<template>
  <div class="app-container">
    <el-card>
      <!--工具栏-->
      <div class="head-container">
        <eHeader :permission="permission"/>
        <crudOperation :permission="permission" />
      </div>
      <!--工具栏-->

      <!--表单组件-->
      <eForm ref="form" />
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="企业名称"/>
        <el-table-column prop="img" label="企业logo" >
          <template slot-scope="scope">
            <el-image :src="baseApi + '/recruit/' + scope.row.img"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="city" label="总部地址" />
        <el-table-column prop="mission" label="公司使命" />
        <el-table-column prop="status" label="状态" >
          <template slot-scope="scope">
            <el-button v-if="scope.row.status == 1" type="warning" size="mini">待审核</el-button>
            <el-button v-else-if="scope.row.status == 2" type="success" size="mini">通过啦</el-button>
            <el-button v-else-if="scope.row.status == 3"type="danger" size="mini">拒绝啦</el-button>
            <el-button v-else type="primary" size="mini">创建</el-button>
          </template>
        </el-table-column>

        <el-table-column  label="操作" width="150px" align="center">
            <template slot-scope="scope">
              <!-- <udOperation
                 :data="scope.row"
                 :permission="permission"
               />-->
               <el-button v-if="scope.row.status === 1" @click="passEvent(scope.row)" type="success" size="mini">通过</el-button>
               <el-button v-if="scope.row.status === 1 && scope.row.status !== 3" @click="rejectEvent(scope.row)" type="warning" size="mini">拒绝</el-button>
            </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </el-card>

    <!--表单渲染-->
    <el-dialog append-to-body
               :close-on-click-modal="false"
               :before-close="cancelFine"
               :visible.sync="fineV"
               title="罚款" width="600px">
      <el-form ref="form" :inline="true" :model="form"  size="small" label-width="86px">
        <el-form-item label="借阅单号" prop="borrowId">
          <el-input v-model="form.borrowId" />
        </el-form-item>
        <el-form-item label="图书名称" prop="name">
          <el-input v-model.number="form.name" />
        </el-form-item>
        <el-form-item label="罚款" prop="money">
          <el-input v-model.number="form.money" />
        </el-form-item>
        <el-form-item label="借阅时间" prop="borrowTime">
          <el-input v-model="form.borrowTime" />
        </el-form-item>
        <el-form-item label="归还时间" prop="returnTime">
          <el-input v-model.number="form.returnTime" />
        </el-form-item>
        <el-form-item label="用户名称" prop="username">
          <el-input v-model="form.username" />
        </el-form-item>
        <el-form-item label="电话" prop="phoneNum">
          <el-input v-model.number="form.phoneNum" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="cancelFine">取消</el-button>
        <el-button type="primary" @click="submitFine(form)">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import crudCompany, { audit } from '@/api/ceamCompany'
import {add} from '@/api/ceamStar'
import eForm from './form'
import eHeader from './header'
import CRUD, { presenter } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import udOperation from '@crud/UD.operation'
import { mapGetters } from 'vuex'

// crud交由presenter持有
const crud = CRUD({
  title: '审核企业',
  url: 'api/company',
  sort: ['sort,asc', 'id,desc'],
  crudMethod: { ...crudCompany }
})

export default {
  components: { eHeader, eForm, crudOperation, pagination, udOperation },
  mixins: [presenter(crud)],
  data() {
    return {
      delLoading: false,
      fineV: false,
      form: {},
      permission: {
        add: ['admin', 'teacher:add'],
        edit: ['admin', 'common','teacher:edit'],
        del: ['admin', 'common', 'teacher:del']
      }
    }
  },
  computed: {
    ...mapGetters([
      'baseApi'
    ])
  },
  created() {
    this.$nextTick(() => {
      //this.init()
    })
  },
  methods: {
    checkPermission,
    cancelFine() {
      this.fineV = false;
      this.form = {};
    },
    passEvent(data) {
      data.status = 2;
      audit(data).then(res => {
        this.$message({
          message: '成功',
          type: 'success'
        });
        //this.init()
      }).catch(error => {
        this.$message({
          message: '失败',
          type: 'warning'
        });
      })
    },
    rejectEvent(data) {
      data.status = 3;
      audit(data).then(res => {
        this.$message({
          message: '成功',
          type: 'success'
        });
        //this.init()
      }).catch(error => {
        this.$message({
          message: '失败',
          type: 'warning'
        });
      })
    },
    submitFine(item) {
      add(item).then(res => {
        this.fineV = false;
        this.$message({
          message: '添加罚金成功',
          type: 'success'
        });
      }).catch(error => {
        this.$message({
          message: '添加罚金失败',
          type: 'warning'
        });
      })
    },
    add() {
      this.isAdd = true
      this.$refs.form.dialog = true
    },
    edit(data) {
      this.isAdd = false
      const _this = this.$refs.form
      _this.form = {
        id: data.id,
        routeCn: data.routeCn,
        name: data.name,
        startPoint: data.startPoint,
        endPoint: data.endPoint,
        description: data.description
      }
      _this.dialog = true
    }
  }
}
</script>

<style scoped>

</style>
